const puppeteer = require("puppeteer-core");
let fs = require("fs");
let path = require("path");
let allData = require("../ai/res/res.json");

module.exports = async function pc() {
  const browser = await puppeteer.launch({
    headless: true, //有浏览器界面启动
    slowMo: 100, //放慢浏览器执行速度，方便测试观察
    ignoreHTTPErrors: true,
    defaultViewport: { width: 1400, height: 700 },
    args: [
      //启动 Chrome 的参数，详见上文中的介绍
      "–no-sandbox",
      // '--start-fullscreen'
    ],
  });
  const page = await browser.newPage();
  await page.goto("https://www.55128.cn/zs/4_26.htm");

  //$('.test').children[1]

  let head = await page.$("thead .sort_up");
  await head?.click();
  //let table = await page.$$("#chartData tr");
  let body = await page.$("#chartData");

  //     }
  // }, head)
  let res = await page.evaluate(function (el) {
    let red = [],
      blue = [],
      oth = [],
      wzjred = [],
      wzjblue = [];
    let firstTR = el.querySelectorAll("tr")[0];
    let secondTR = el.querySelectorAll("tr")[1];
    let td = firstTR.querySelectorAll("td");
    let td2 = secondTR.querySelectorAll("td");
    //console.log(td);
    let line = 0;
    let time = "";
    td.forEach((item) => {
      console.log(item, "item");
      // 分割线
      if (item.className.includes("split02")) {
        line++;
      }
      if (item.className.includes("brl")) {
        time = item.innerText;
      }
      //红色中将求
      if (item.getAttribute("data-ctr") != null) {
        red.push(item.innerText - 0);
      }
      //绿色中将求
      if (item.className.includes("chartball_blue")) {
        blue.push(item.innerText - 0);
      }
      if (line > 0 && line < 4) {
        //红色未中奖求
        item.className.includes("miss-yred") && wzjred.push(item.innerText);
      }
      if (line >= 4 && line < 5) {
        item.className.includes("miss-yred") && wzjblue.push(item.innerText);
      }
      if (line >= 5) {
        item.innerText && oth.push(item.innerText);
      }
    });
    let oldRed = [],
      oldBlue = [],
      oldTime = "";
    td2.forEach((item) => {
      if (item.className.includes("brl")) {
        oldTime = item.innerText;
      }
      //红色中将求
      if (item.getAttribute("data-ctr") != null) {
        oldRed.push(item.innerText - 0);
      }
      //绿色中将求
      if (item.className.includes("chartball_blue")) {
        oldBlue.push(item.innerText - 0);
      }
    });
    console.log("红色中奖求", red);
    console.log("蓝色中将球", blue);
    console.log("红色未中奖球排列", wzjred);
    console.log("蓝色未中奖排列", wzjblue);

    console.log("上一期红色中奖", oldRed);
    console.log("上一期蓝色中奖", oldBlue);
    return {
      time,
      red,
      blue,
      wzjred,
      wzjblue,
      oldTime,
      oldRed,
      oldBlue,
      oth,
    };
  }, body);
  //let clures=await page
  // await p()
  if (!fs.existsSync(path.join(__dirname, "./img"))) {
    fs.mkdirSync(path.join(__dirname, "./img"));
  }

  await page.screenshot({ path: path.join(__dirname, "./img/cp.png") });
  browser.close();
  res.wzjred = res.wzjred.map((i) => i - 0 + 1);
  res.wzjblue = res.wzjblue.map((i) => i - 0 + 1);

  for (let i of res.red) {
    res.wzjred.splice(i - 1, 0, 1);
  }
  for (let i of res.blue) {
    res.wzjblue.splice(i - 1, 0, 1);
  }
  if (!allData[res.time]) {
    allData[res.time] = {
      red: res.red,
      blue: res.blue,
      wzjred: res.wzjred,
      wzjblue: res.wzjblue,
      oth: res.oth,
    };
    fs.writeFile(
      path.join(__dirname, "../ai/res/res.json"),
      JSON.stringify(allData, null, 2),
      { flag: "w" },
      () => {}
    );
  }
  return res;
};

function p() {
  return new Promise((res) => {
    setTimeout(() => {
      res();
    }, 1000);
  });
}
